package com.china08.yxyapi.api.home.activity.marker;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.maker.YxyMakerCatalog;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.marker.YxyMakerCatalogRepository;
import com.china08.yxyapi.repository.db.marker.YxyMakerWorksRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

/**
 * 区县教育局-竞赛报名-点击立即报名判断报名人数是否已达上限
 * 
 * @author zd.Jin
 *
 *
 */
@RestController
@CrossOrigin
public class YxyMakerWorksCheckCountController extends YxyApiBaseController {
    @Autowired
    private YxyMakerCatalogRepository yxyMakerCatalogRepository;
    @Autowired
    private YxyMakerWorksRepository yxyMakerWorksRepository;

    @RequestMapping(value = "/api_yw/home/activity/competition/catalog/check", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        RespModel respModel = new RespModel();
        // 得到当前登录用户
        YxyProfile profile = getProfile(request, response);
        String schoolId = profile.getSchoolId();
        YxyMakerCatalog ca = yxyMakerCatalogRepository.findOne(reqModel.id);
        int pid = Integer.parseInt(ca.getPid());
        if (pid < 10) {// 评选类、创客竞赛按组别（小、初、高每个组别限报多少人）
            YxyMakerCatalog pca = yxyMakerCatalogRepository.findOne(ca.getPid());
            int count = yxyMakerWorksRepository.countBySchoolIdAndCatalog1AndPhase(schoolId, ca.getPid(),
                    reqModel.phase);
            if (pca.getLimitCount() > count) {
                respModel.flag = 1;
            } else {
                respModel.flag = 2;
            }
        } else {// 机器人、益智类、学科类按项目（每个项目限报多少人）
            int count = yxyMakerWorksRepository.countBySchoolIdAndCatalog2(schoolId, reqModel.id);
            if (ca.getLimitCount() > count) {
                respModel.flag = 1;
            } else {
                respModel.flag = 2;
            }
        }
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

        public String phase;// 学段 小学：1 初中：2 高中：3 中职：4
        public String id;// 创客分类的id

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(id, null), "创客分类的id不能为空");
            Assert.notNull(StringUtils.defaultIfBlank(phase, null), "学段不能为空");
        }

    }

    protected static class RespModel extends YxyApiRespModel {
        // 1代表可以报名 2代表报名人数已达上限
        public Integer flag;
    }
}
